//
//  UILabel+ACCommon.m
//  AcmedcareCore
//
//  Created by AcZx on 2018/3/9.
//

#import "UILabel+ACCommon.h"

@implementation UILabel (ACCommon)

+ (instancetype)label
{
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    return label;
}

- (CGSize)contentSize
{
    return [self textSizeIn:self.bounds.size];
}

- (CGSize)textSizeIn:(CGSize)size
{
    NSLineBreakMode breakMode = self.lineBreakMode;
    UIFont *font = self.font;
    
    CGSize contentSize = CGSizeZero;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = breakMode;
    paragraphStyle.alignment = self.textAlignment;
    
    NSDictionary* attributes = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:paragraphStyle};
    contentSize = [self.text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
    
    contentSize = CGSizeMake((int)contentSize.width + 1, (int)contentSize.height + 1);
    
    return contentSize;
}

+ (instancetype)labelWith:(NSString *)text;
{
    UILabel *label = [UILabel label];
    label.text = text;
    return label;
}

+ (instancetype)labelWith:(NSString *)text textColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = color;
    return label;
}

+ (instancetype)labelWith:(NSString *)text textColor:(UIColor *)color backgroundColor:(UIColor *)bgColor
{
    UILabel *label = [UILabel labelWith:text textColor:color];
    label.backgroundColor = bgColor;
    return label;
}

+ (instancetype)centerlabelWith:(NSString *)text
{
    UILabel *label = [UILabel labelWith:text];
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}


+ (instancetype)labelWith:(NSString *)text font:(CGFloat)size
{
    UILabel *label = [UILabel labelWith:text];
    label.font = [UIFont systemFontOfSize:size];
    return label;
}

+ (instancetype)labelWith:(NSString *)text font:(CGFloat)size textColor:(UIColor *)textColor
{
    UILabel *label = [UILabel labelWith:text font:size];
    label.textColor = textColor;
    return label;
}

+ (instancetype)labelWith:(NSString *)text boldFont:(CGFloat)size
{
    UILabel *label = [UILabel labelWith:text];
    label.font = [UIFont boldSystemFontOfSize:size];
    return label;
}

+ (instancetype)centerlabelWith:(NSString *)text font:(CGFloat)size
{
    UILabel *label = [UILabel centerlabelWith:text];
    label.font = [UIFont systemFontOfSize:size];
    return label;
}

+ (instancetype)centerlabelWith:(NSString *)text font:(CGFloat)size textColor:(UIColor *)textColor
{
    UILabel *label = [UILabel centerlabelWith:text font:size];
    label.textColor = textColor;
    label.adjustsFontSizeToFitWidth = YES;
    return label;
}

+ (instancetype)centerlabelWith:(NSString *)text boldFont:(CGFloat)size
{
    UILabel *label = [UILabel centerlabelWith:text];
    label.font = [UIFont boldSystemFontOfSize:size];
    return label;
}

@end


@implementation InsetLabel


- (void)drawTextInRect:(CGRect)rect
{
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, _contentInset)];
}


- (CGSize)contentSize
{
    CGRect rect = UIEdgeInsetsInsetRect(self.bounds, _contentInset);
    CGSize size = [super textSizeIn:rect.size];
    return CGSizeMake(size.width + _contentInset.left + _contentInset.right, size.height + _contentInset.top + _contentInset.bottom);
}

- (CGSize)textSizeIn:(CGSize)size
{
    size.width -= _contentInset.left + _contentInset.right;
    size.height -= _contentInset.top + _contentInset.bottom;
    CGSize textSize = [super textSizeIn:size];
    return CGSizeMake(textSize.width + _contentInset.left + _contentInset.right, textSize.height + _contentInset.top + _contentInset.bottom);
}


@end


